package day04;

/*
 * Author: JacksonCoder
 * FileName: EqualsDemo
 * Version: 1.0.0
 * Date: 2025/4/11
 */

class User{
    private String name;

    private int age;

    public User(String name, int age){
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj){
        if(this == obj) return true;  // 检查是否是同一个对象
        if(obj == null || getClass() != obj.getClass()) return false; // 检查对象是否为null或类型是否相同

        User user = (User) obj; // 类型转换

        // 比较字段
        if(age != user.age) return false;
        return name != null ? name.equals(user.name) : user.name == null;
    }

    @Override
    public int hashCode(){
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    // Getters and setter
    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public int getAge(){
        return age;
    }

    public void setAge(int age){
        this.age = age;
    }

}

public class EqualsDemo {

    public static void main(String[] args) {
        User user1 = new User("alen",134);
        User user2 = new User("Nexora",230);
        User user3 = new User("Bob",944);
        User user4 = new User("Bob",944);

        System.out.println(user1.equals(user2)); // false
        System.out.println(user3.equals(user4)); // true
    }

}
